﻿using UnityEngine;
using System.Collections;

public enum EndUIState
{
	nothing,
	referee,
	comment,
	evaluate,
	eventView,
}

public class LV1End : MonoBehaviour {
	protected EndUIState state;
	public UI2DSprite spReferee;
	public AudioSource sdReferee;
    public UI2DSprite spComment;
    public UI2DSprite CmPft;
    public UI2DSprite CmNml;
    public UI2DSprite CmFail;
	public AudioSource sdComment;
    public UI2DSprite spEvaluate;
    public UI2DSprite EvPft;
    public UI2DSprite EvNml;
    public UI2DSprite EvFail;
	public AudioSource sdEvaluate;
	public AudioClip EvaluatePft;
	public AudioClip EvaluateNml;
	public AudioClip EvaluateFail;
	public AudioSource sdEvent;
	public AudioClip EventPft;
	public AudioClip EventNml;
	public AudioClip EventFail;
    public AudioSource EndMusic;
	public UIWidget wdtEvlt;     //Widget1
	public UIWidget wdtEvt;      //Widget2
	public UI2DSprite spEventGood;
	public UI2DSprite spEventFail;
    public UI2DSprite spEventNormal;
	public UILabel testlebel;


	// Use this for initialization
	void Start () {
		state = EndUIState.nothing;


		if (LevelScore.Perfect == PlayerScore.GetScore ()) {
			testlebel.text = "perfect";
			sdEvaluate.clip = EvaluatePft;
            sdEvent.clip = EventPft;
            spComment = CmPft;
            spEvaluate = EvPft;
			spEventGood.gameObject.SetActive(true);
            spEventNormal.gameObject.SetActive(false);
			spEventFail.gameObject.SetActive(false);
		} else if (LevelScore.normal == PlayerScore.GetScore ()) {
			testlebel.text = "normal";
			sdEvaluate.clip = EvaluateNml;
            sdEvent.clip = EventNml;
            spComment = CmNml;
            spEvaluate = EvNml;
			spEventGood.gameObject.SetActive(false);
            spEventNormal.gameObject.SetActive(true);
			spEventFail.gameObject.SetActive(false);
		}else if(LevelScore.fail == PlayerScore.GetScore()){
			testlebel.text = "fail";
			sdEvaluate.clip = EvaluateFail;
            sdEvent.clip = EventFail;
            spComment = CmFail;
            spEvaluate = EvFail;
			spEventGood.gameObject.SetActive(false);
            spEventNormal.gameObject.SetActive(false);
			spEventFail.gameObject.SetActive(true);
		}

		spReferee.enabled = false;
        spEvaluate.enabled = false;
		EvPft.enabled = false;
        EvNml.enabled = false;
        EvFail.enabled = false;
        spComment.enabled = false;
        CmPft.enabled = false;
        CmNml.enabled = false;
        CmFail.enabled = false;
		wdtEvt.gameObject.SetActive(false);//Widget2不显示
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown ("Fire1"))
			MoveNextState ();
	}

	public void MoveNextState()
	{
		if (state == EndUIState.nothing) {
			Mov2Referee();
		}else if (state == EndUIState.referee) {
			Mov2Comment();
		}else if (state == EndUIState.comment){
			Mov2Evaluate();
		}else if (state == EndUIState.evaluate){
			Mov2Event();
		}
	}

	void Mov2Referee()
	{
		spReferee.enabled = true;
		// play effect sound
		sdReferee.Play ();
		state = EndUIState.referee;
	}

	void Mov2Comment()
	{
		spComment.enabled = true;
		// play effect sound
		sdComment.Play ();
		state = EndUIState.comment;
	}

	void Mov2Evaluate()
	{
		spEvaluate.enabled = true;
		// play effect sound
		sdEvaluate.Play ();
		state = EndUIState.evaluate;
        EndMusic.Play();
	}

	void Mov2Event()
	{
        EndMusic.Stop();
		wdtEvt.gameObject.SetActive (true);     //让Widget2显示
        wdtEvlt.gameObject.SetActive(false);    //Widget1不显示
		// play effect sound
		sdEvent.Play ();
		state = EndUIState.eventView;
	}
}
